﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using cpe.cpein.DATA;

namespace cpe.cpein.BUSINESS
{
    public static class BUStudy
    {
        /// <summary>
        /// Get All the Study of the database.
        /// </summary>
        /// <param name="searchInDeleted"></param>
        /// <returns></returns>
        public static List<Study> GetAllStudy(bool searchInDeleted)
        {
            List<Study> Studys = new List<Study>();
            using (cpeinEntities db = new cpeinEntities())
            {
                var results = from Study in db.Study
                              where Study.std_isdeleted == searchInDeleted
                              select Study;
                foreach (Study tmp in results)
                    Studys.Add(tmp);
            }

            return Studys;
        }

        /// <summary>
        /// Get the Study by id.
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static Study GetStudyByID(int id)
        {
            if (id <= 0)
                return null;
            else
            {
                using (cpeinEntities db = new cpeinEntities())
                {
                    var std = from Study in db.Study
                              where Study.std_id == id
                              select Study;
                    foreach (Study tmp in std)
                        return tmp;
                    return null;
                }
            }
        }

        /// <summary>
        /// std a Study into the database.
        /// </summary>
        /// <param name="std"></param>
        /// <returns></returns>
        public static Study Studyrea(Study std)
        {
            if (std != null)
            {
                using (cpeinEntities db = new cpeinEntities())
                {
                    Study nstd = db.Study.Add(std);
                    db.SaveChanges();
                    return nstd;
                }
            }
            else
                return null;
        }

        /// <summary>
        /// Remove a specific Study of the database.
        /// </summary>
        /// <param name="std"></param>
        /// <returns></returns>
        public static bool RemoveStudy(Study std)
        {
            if (std != null)
            {
                std.std_isdeleted = true;
                if (UpdateStudy(std) != null)
                {
                    using (cpeinEntities db = new cpeinEntities())
                    {
                        db.SaveChanges();
                        return true;
                    }
                }
            }

            return false;
        }

        /// <summary>
        /// Update the Study into the database.
        /// </summary>
        /// <param name="std"></param>
        /// <returns></returns>
        public static Study UpdateStudy(Study std)
        {
            if (std != null)
            {
                using (cpeinEntities db = new cpeinEntities())
                {
                    var current = from Study in db.Study
                                  where Study.std_id == std.std_id
                                  select Study;

                    foreach (var tmp in current)
                    {
                        tmp.grp_id = std.grp_id;
                        tmp.std_label = std.std_label;
                        tmp.std_short_label = std.std_short_label;
                        tmp.std_isdeleted = std.std_isdeleted;

                        db.SaveChanges();
                    }

                    std = current as Study;
                }
            }

            return std;
        }
    }
}
